package com.example.cd5160866.modelrecycleview.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

/**
 * Created by cd5160866 on 16/5/10.
 */
public class ModelRecyclerAdapterV2<T> extends RecyclerView.Adapter<ModelRecyclerAdapterV2.ModelViewHolder> {
    public static final int TYPE_DEFAULT = 1000;
    public static final int TYPE_HEADER = 1001;
    public static final int TYPE_FOOTER = 1002;
    private Context mContext;
    private int mLayoutId;
    private Class<? extends ModelViewHolder> viewHolderClass;
    private List<T> mData = new ArrayList<>();
    private int itemSize = 0;
    private View headerView;
    private View footerView;
    private Object object;

    public ModelRecyclerAdapterV2(Context context, Class<? extends ModelViewHolder<T>> viewHolderClass) {
        this(context, viewHolderClass, new ArrayList<T>());
    }

    public ModelRecyclerAdapterV2(Context context, Class<? extends ModelViewHolder<T>> viewHolderClass, List<T> data) {
        this.mContext = context;
        this.viewHolderClass = viewHolderClass;
        this.mLayoutId = viewHolderClass.getAnnotation(RecyclerItemViewId.class)
                .value();
        this.mData = data;
    }

    @Override
    public ModelViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
        ModelViewHolder viewHolder;
        View convertView;
        switch (viewType) {
            case TYPE_HEADER:
                convertView = headerView;
                break;
            case TYPE_FOOTER:
                convertView = footerView;
                break;
            default:
                convertView = LayoutInflater.from(mContext).inflate(mLayoutId, parent, false);
                break;
        }

        try {
//            viewHolder = (ModelViewHolder) (viewHolderClass.getConstructors()[0]).newInstance(getContext(), convertView);
                        viewHolder = viewHolderClass.getConstructor(View.class).newInstance(convertView);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("ViewHolder must has a custom (param View type ) constructor");
        }

        viewHolder.viewType = viewType;
        if (viewType == TYPE_DEFAULT) {
            ButterKnife.bind(viewHolder, convertView);
        }

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ModelViewHolder holder, int position) {
        if (null == mData || mData.size() == 0) {
            return;
        }
        if (holder.viewType == TYPE_DEFAULT) {
            if (headerView != null) {
                position = position - 1;
            }
            holder.update(mData.get(position), this, position);
        }
    }

    @Override
    public int getItemViewType(int position) {
        int itemViewType = TYPE_DEFAULT;
        if (itemSize == 0) {
            return itemViewType;
        }
        if (position == 0) {
            if (headerView != null) {
                itemViewType = TYPE_HEADER;
            }
        } else if (position == itemSize - 1) {
            if (footerView != null) {
                itemViewType = TYPE_FOOTER;
            }
        }
        return itemViewType;
    }

    @Override
    public int getItemCount() {
        itemSize = mData.size();
        if (headerView != null) {
            itemSize++;
        }
        if (footerView != null) {
            itemSize++;
        }
        return itemSize;
    }

    public Context getContext() {
        return mContext;
    }

    public List<T> getItems() {
        return mData;
    }


    /**
     * 添加了header 或者footer 的时候 获取item 可能会为空
     *
     * @param layoutPosition 必须调用 viewHolder.getLayoutPosition的方法 header 和footer 也会计入layoutPosition中
     */
    @Nullable
    public T getItem(int layoutPosition) {
        if (getItemViewType(layoutPosition) != TYPE_DEFAULT) {
            return null;
        }

        if (null != headerView) {
            //header 的数量
            layoutPosition = layoutPosition - 1;
        }

        if (Arrays.isEmpty(mData) || layoutPosition < 0 || layoutPosition >= mData.size()) {
            return null;
        }
        return mData.get(layoutPosition);
    }

    public void add(T data) {
        mData.add(data);
        notifyDataSetChanged();
    }

    public void add(int position, T data) {
        mData.add(position, data);
        notifyDataSetChanged();
    }

    public void remove(int position) {
        mData.remove(position);
        notifyDataSetChanged();
    }


    public void setItems(List<T> data) {
        mData.clear();
        mData.addAll(data);
        notifyDataSetChanged();

    }

    public void addAll(List<T> data) {
        mData.addAll(data);
        notifyDataSetChanged();
    }

    public void clear() {
        mData.clear();
        notifyDataSetChanged();
    }

    public Object getObject() {
        return object;
    }

    public void setObject(Object object) {
        this.object = object;
    }

    public View getHeaderView() {
        return headerView;
    }

    public void setHeaderView(View headerView) {
        this.headerView = headerView;
        this.notifyDataSetChanged();
    }

    public View getFooterView() {
        return footerView;
    }

    public void setFooterView(View footerView) {
        this.footerView = footerView;
        this.notifyDataSetChanged();
    }

    public static abstract class ModelViewHolder<T> extends RecyclerView.ViewHolder {

        int viewType;

        public ModelViewHolder(View itemView) {
            super(itemView);
        }

        public abstract void update(T item, ModelRecyclerAdapterV2 adapter, int position);
    }
}
